/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"errors"
	"github.com/rs/zerolog/log"
	"path/filepath"
	"v3workshop/app"

	"github.com/spf13/cobra"
)

// setCmd represents the set command
var setCmd = &cobra.Command{
	Use:   "set",
	Short: "设置配置信息",
	Long: `
配置默认的数据源和目标路径, 后续merge会自动数据源目录获取mod包, 并将所选mod包拷贝到目标路径进行合并
`,
	Args: func(cmd *cobra.Command, args []string) error {
		if setOpt.SourcePath == "" {
			return errors.New("源路径不能为空")
		}
		if setOpt.TargetPath == "" {
			return errors.New("目标路径不能为空")
		}
		abs, err := filepath.Abs(setOpt.SourcePath)
		if err != nil {
			return err
		}
		setOpt.SourcePath = abs
		abs, err = filepath.Abs(setOpt.TargetPath)
		if err != nil {
			return err
		}
		setOpt.TargetPath = abs
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		m := app.NewManager()
		err := m.Set(setOpt)
		if err != nil {
			log.Err(err).Msg("设置失败")
			return
		}
		log.Info().Msg("设置成功")
	},
}

var setOpt = new(app.ConfigSetOption)

func init() {
	rootCmd.AddCommand(setCmd)

	setCmd.Flags().StringVarP(&setOpt.SourcePath, "source", "s", "", "源路径")
	setCmd.Flags().StringVarP(&setOpt.TargetPath, "target", "t", "", "目标路径")
}
